package com.db.compare.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSONObject;
import com.db.compare.exp.RbacMenuExp;
import com.db.compare.model.RbacMenu;
import com.db.compare.mapper.RbacMenuMapper;
import com.db.compare.service.RbacMenuService;
import com.db.compare.vo.RbacMenuVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class RbacMenuServiceImpl implements RbacMenuService {
    @Autowired
    private RbacMenuMapper rbacmenuMapper;
    @Override
    public PageInfo<RbacMenu> pageInfo(RbacMenuVo rbacmenuVo) {
        PageHelper.startPage(rbacmenuVo.getPageNum(),rbacmenuVo.getPageSize());
        List<RbacMenu> rbacmenuList = rbacmenuMapper.findList(rbacmenuVo);
        PageInfo<RbacMenu> pageInfo = new PageInfo<>(rbacmenuList);
        return pageInfo;
    }

    @Override
    public void download(String ids, HttpServletResponse response) throws Exception {
        String fileName = "菜单管理";
        try {
            ServletOutputStream outputStream=Appcontext.downloadResponse(response,fileName);
            if(StringUtils.isBlank(ids)){
                EasyExcel.write(outputStream, RbacMenuExp.class).sheet(fileName).doWrite(null);
            }else{
                String[] arrays = ids.split(",");
                List<RbacMenu> rbacmenuList = rbacmenuMapper.findByIds(arrays);
                List<RbacMenuExp> rbacmenuExpList = new ArrayList<>();
                for(RbacMenu rbacmenu:rbacmenuList){
                    RbacMenuExp rbacmenuExp = new RbacMenuExp();
                    BeanUtils.copyProperties(rbacmenu,rbacmenuExp);
                    rbacmenuExpList.add(rbacmenuExp);
                }
                EasyExcel.write(outputStream, RbacMenuExp.class).sheet(fileName).doWrite(rbacmenuExpList);
            }
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean upload(MultipartFile file) {
        ImportParams importParams = new ImportParams();
        try {
            ExcelImportResult<RbacMenuExp> result = ExcelImportUtil.importExcelMore(file.getInputStream(), RbacMenuExp.class,
                    importParams);
            List<RbacMenuExp> rbacmenuExps = result.getList();
            for (RbacMenuExp rbacmenuExp : rbacmenuExps) {
                RbacMenu rbacmenu = new RbacMenu();
                BeanUtils.copyProperties(rbacmenu,rbacmenuExp);
                int i = rbacmenuMapper.insert(rbacmenu);
                if(i < 1){
                    return false;
                }
            }
        } catch (Exception e) {
            return false;
        }
        return true;
    }
}
